package com.backup;

import com.wizardOther.Items.ValidationForm;
import configurationReader.Config;
import configurationReader.ConfigReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Krzysztof
 */
public class BackupRestore extends ValidationForm {

    private String mysqldPathInFileSystem;
    private String dbName;
    private String dbUserName;
    private String dbPassword;
    private String backupPath;
    private String restorePath;

    public BackupRestore() {
        this.mysqldPathInFileSystem = Config.INSTANCE.getXampLocation();
        this.dbName = Config.INSTANCE.getXamppDbName();
        this.dbUserName = Config.INSTANCE.getXamppDbUser();
        this.dbPassword = Config.INSTANCE.getXamppDbPassword();
    }

    public String getMysqldPathInFileSystem() {
        return mysqldPathInFileSystem;
    }

    public void setMysqldPathInFileSystem(String mysqldPathInFileSystem) {
        this.mysqldPathInFileSystem = mysqldPathInFileSystem;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbUserName() {
        return dbUserName;
    }

    public String getBackupPath() {
        return backupPath;
    }

    public void setBackupPath(String backupPath) {
        this.backupPath = backupPath;
        System.out.println(backupPath);
    }

    public String getRestorePath() {
        return restorePath;
    }

    public void setRestorePath(String restorePath) {
        this.restorePath = restorePath;
    }

    public void setDbUserName(String dbUserName) {
        this.dbUserName = dbUserName;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }

    public boolean backupDB() {
        boolean result = false;
        String executeCmd = mysqldPathInFileSystem + "mysqldump -u " + dbUserName + " --add-drop-database -B " + dbName + " -r " + backupPath;
        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);

            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                Logger.getLogger(ConfigReader.class.getName()).log(Level.SEVERE, "[CarPark] Backup przebiegł pomyślnie.");
                return true;
            } else {
                Logger.getLogger(ConfigReader.class.getName()).log(Level.SEVERE, "[CarPark] Backup error. Cannot connect to db.");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

    public boolean restoreDB() {

        String[] executeCmd = new String[]{mysqldPathInFileSystem + "mysql", "--user=" + dbUserName, "-e", "source " + restorePath};

        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Kopia przywrócona pomyślnie");
                return true;
            } else {
                System.out.println("Wystąpil problem podczas przywracania");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return false;
    }

}
